package com.caigou.controller;

import com.caigou.bean.cms.entity.UserBaseInfo;
import com.caigou.bean.cms.param.UserHasRecycleParam;
import com.caigou.bean.cms.param.UsersUpdateParam;
import com.caigou.bean.param.FunctionStaffPrivilegeParam;
import com.caigou.bean.vo.FunctionPrivilegeVO;
import com.caigou.bean.vo.PrivilegeVO;
import com.caigou.bean.vo.StaffPrivilegesVO;
import com.caigou.bean.vo.UserBaseInfoVO;
import com.caigou.bean.exam.entity.Users;
import com.caigou.bean.param.StaffManageParam;
import com.caigou.bean.param.UserBaseInfoParam;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.IStaffManageService;
import com.caigou.service.IUsersService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffManageController {

    
    @Resource
    private IStaffManageService staffManageService;
    @Resource
    private IUsersService usersService;

    /**
     * 查询员工信息列表
     *
     * @param user
     * @return
     */
    @PostMapping("/list/query")
    @PreAuthorize("hasAnyAuthority('yuangongchaxun')")
    public Result<PageInfo<UserBaseInfo>> findStaffByParam(@RequestBody StaffManageParam param, @CurrentUser Users user) {
        return staffManageService.findStaffByParam(param);
    }

    /**
     * 根据员工staff_id查询员工详情信息
     *
     * @param user
     * @return
     */
    @PostMapping("/info/query")
    @PreAuthorize("hasAnyAuthority('yuangongchaxun')")
    public Result<UserBaseInfoVO> findStaffByStaffId(@RequestBody UserBaseInfoParam param, @CurrentUser Users user) {
        return staffManageService.findStaffByStaffId(param);
    }

    /**
     * 编辑员工信息
     *
     * @param user
     * @return
     */
    @PostMapping("/info/update")
    @PreAuthorize("hasAnyAuthority('yuangongguanli')")
    public Result<Integer> updateStaffByParam(@RequestBody UserBaseInfoParam param, @CurrentUser Users user) {
        return staffManageService.updateStaffByParam(param);
    }


    /**
     * 新增员工信息
     *
     * @param user
     * @return
     */
    @PostMapping("/info/add")
    @PreAuthorize("hasAnyAuthority('yuangongguanli')")
    public Result<String> addStaffByParam(@RequestBody UserBaseInfoParam param, @CurrentUser Users user) {
        return staffManageService.addStaffByParam(param);
    }


    /**
     * 查询员工后台管理左侧菜单栏
     *
     * @param
     * @param user
     * @return
     */
    @PostMapping("/privileges")
    public Result<List<PrivilegeVO>> findStaffPrivilegesByUserId(@CurrentUser Users user) {
        return usersService.findStaffPrivilegesByUserId(user.getUserid());
    }


    /**
     * 更新用户手机号
     * @param param
     * @param user
     * @return
     */
    @RequestMapping("/update/tel")
    @PreAuthorize("hasAnyAuthority('yonghuguanliquan')")
    public Result updateStaffTel(@RequestBody UsersUpdateParam param, @CurrentUser Users user) {
        return usersService.updateStaffTel(param);
    }


    /**
     * 查询用户是否有回收站权限
     *
     * @param user
     * @return
     */
    @RequestMapping("/recycle/authority")
    public Result<Boolean> getUserHasRecycleAuthority(@RequestBody UserHasRecycleParam param , @CurrentUser Users user) {
        if (param.getPrivilegeType()==null) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return usersService.findUserHasRecycleAuthority(param.getPrivilegeType(),user.getUserid());
    }


}
